package top.yokey.shopnc.activity.mine;

import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;

import top.yokey.base.base.BaseCountTime;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.util.JsonUtil;
import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseImageLoader;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.shopnc.bean.InviteIndexBean;
import top.yokey.shopnc.model.MemberInviteModel;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_mine_invite)
public class InviteActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;

    @ViewInject(R.id.qrCodeImageView)
    private AppCompatImageView qrCodeImageView;
    @ViewInject(R.id.linkEditText)
    private AppCompatEditText linkEditText;

    private InviteIndexBean inviteIndexBean;

    @Override
    public void initData() {

        setToolbar(mainToolbar, "邀请返利");
        getData();

    }

    @Override
    public void initEven() {

    }

    //自定义方法

    private void getData() {

        MemberInviteModel.get().index(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                String data = JsonUtil.getDatasString(baseBean.getDatas(), "member_info");
                inviteIndexBean = JsonUtil.json2Bean(data, InviteIndexBean.class);
                BaseImageLoader.get().display(inviteIndexBean.getMyurlSrc(), qrCodeImageView);
                linkEditText.setText(inviteIndexBean.getMyurl());
                linkEditText.setSelection(linkEditText.getText().length());
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getData();
                    }
                }.start();
            }
        });

    }

}
